package com.hlf.modules.wechat.service.impl;

import com.hlf.common.constants.Constants;
import com.hlf.common.enums.WechatTemplateEnum;
import com.hlf.common.utils.wx.TemplateData;
import com.hlf.common.utils.wx.WechatTemplate;
import com.hlf.common.utils.wx.WxUtils;
import com.hlf.modules.base.service.impl.BaseServiceImpl;
import com.hlf.modules.wechat.mapper.OfficialAccountMapper;
import com.hlf.modules.wechat.model.OfficialAccount;
import com.hlf.modules.wechat.model.WechatMessage;
import com.hlf.modules.wechat.service.OfficialAccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * 公众号和开放平台服务层实现类
 * 
 * @author shuliangxing
 *
 * @date 2017年4月19日 下午3:34:13
 */
@Service
public class OfficialAccountServiceImpl extends BaseServiceImpl<OfficialAccount, OfficialAccountMapper>
        implements OfficialAccountService {

    private static final Logger LOGGER = LoggerFactory.getLogger(OfficialAccountServiceImpl.class);
    
    @Autowired
    OfficialAccountMapper mapper;

    @Override
    public OfficialAccount selectByOriginalId(String originalId) {
        return mapper.selectByOriginalId(originalId);
    }

    @Override
    public String getOpenIdByUserIdAndBizType(Long userId, int bizType) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("userId", userId);
        map.put("bizType", bizType);
        return mapper.getOpenIdByUserIdAndBizType(map);
    }

    @Override
    public void pushMessageToMerchantUser(WechatMessage message) {
        String openId = getOpenIdByUserIdAndBizType(message.getUserId(), Constants.SUBSCRIBE_BIZ_TYPE_M);
        try {
            WxUtils.pushMessage(WxUtils.getMerchantAccessTokenValue(message.getTopAgentId()), openId, message.getContent());
        } catch (Exception e) {
            LOGGER.error("给商家用户推送消息异常, openId = {}, 消息内容：{}", openId, message, e);
        }
    }

    @Override
    public void pushMessageToCustomer(WechatMessage message) {
        String openId = getOpenIdByUserIdAndBizType(message.getUserId(), Constants.SUBSCRIBE_BIZ_TYPE_C);
        try {
            WxUtils.pushMessage(WxUtils.getPersonalAccessTokenValue(message.getTopAgentId()), openId, message.getContent());
        } catch (Exception e) {
            LOGGER.error("给消费者用户推送消息异常, openId = {}, 消息内容：{}", openId, message, e);
        }
    }

    @Override
    public void pushTemplateMessageToMerchantUser(Long topAgentId, Long userId, Map<String, TemplateData> data, WechatTemplateEnum templateEnum) {
        try {
            WechatTemplate wechatTemplate = new WechatTemplate();
            String accessToken = WxUtils.getMerchantAccessTokenValue(topAgentId);
            String openId = getOpenIdByUserIdAndBizType(userId, Constants.SUBSCRIBE_BIZ_TYPE_M);
            
            wechatTemplate.setTouser(openId);
            wechatTemplate.setTemplate_id(WxUtils.getWechatTemplateId(topAgentId, templateEnum));
            wechatTemplate.setUrl(null);
            wechatTemplate.setData(data);
            WxUtils.sendTemplateMsg(accessToken, wechatTemplate);
        } catch (Exception e) {
            LOGGER.error(templateEnum+"推送模板消息异常，topAgentId = {}, userId = {}, data = {}", topAgentId, userId, data, e);
        }
    }

    @Override
    public void pushTemplateMessageToPersonalUser(Long topAgentId, Long userId, Map<String, TemplateData> data,WechatTemplateEnum templateEnum) {
        try {
            WechatTemplate wechatTemplate = new WechatTemplate();
            String accessToken = WxUtils.getPersonalAccessTokenValue(topAgentId);
            String openId = getOpenIdByUserIdAndBizType(userId, Constants.SUBSCRIBE_BIZ_TYPE_C);
            
            wechatTemplate.setTouser(openId);
            wechatTemplate.setTemplate_id(WxUtils.getWechatTemplateId(topAgentId, templateEnum));
            wechatTemplate.setUrl(null);
            wechatTemplate.setData(data);
            WxUtils.sendTemplateMsg(accessToken, wechatTemplate);
        } catch (Exception e) {
            LOGGER.error(templateEnum+"推送模板消息异常，topAgentId = {}, userId = {}, data = {}", topAgentId, userId, data, e);
        }
    }
}
